package at.fhooe.mc.task01;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class BinarySearchTreeTest {

	BinarySearchTree t = new BinarySearchTree();

	@Before
	public void setUp() throws Exception {
		t.insert(10);
		t.insert(15);
		t.insert(8);
		t.insert(7);
		t.insert(-5);
		t.insert(-31);
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testInsert() {
		
		BinarySearchTree test = new BinarySearchTree();

		test.insert(28);
		test.insert(16);
		test.insert(34);
		test.insert(12);
		test.insert(19);
		test.insert(8);
		test.insert(15);
		test.insert(31);
		test.insert(49);
		test.insert(29);
		
		assertEquals(10, test.size);
		assertEquals(16, test.getParent(19));
		assertTrue(test.find(49));
		assertFalse(test.find(50));
		assertEquals(8,test.min());
		assertEquals(49,test.max());
		
		assertEquals(10,test.elements(test.root, 0));
	}

	@Test
	public void testFind() {

		assertTrue(t.find(15));
		assertTrue(t.find(-31));
		assertTrue(t.find(8));
		assertFalse(t.find(0));
	}

	@Test
	public void testRemove() {
		BinarySearchTree test = new BinarySearchTree();

		test.insert(28);
		test.insert(16);
		test.insert(34);
		test.insert(12);
		test.insert(19);
		test.insert(8);
		test.insert(15);
		test.insert(31);
		test.insert(49);
		test.insert(29);
		
		int[] test1 = new int[] {28, 16, 12, 8, 15, 19, 34, 31, 29, 49 };
		assertArrayEquals(test1, test.toArrayPreOrder());
		
		
		assertTrue(test.remove(12));
		int[] test2 = new int[] {28, 16, 15, 8, 19, 34, 31, 29, 49};
		assertArrayEquals(test2, test.toArrayPreOrder());
		
		int[] test3= new int[] {10,8,-5,-31,15};
		assertTrue(t.remove(7));
		assertArrayEquals(test3,t.toArrayPreOrder());
		
		
	}

	@Test
	public void testSize() {

		assertEquals(6, t.size());

	}

	@Test
	public void testGetParent() {

		assertEquals(7, t.getParent(-5));
		assertEquals(10, t.getParent(15));
		assertEquals(-5, t.getParent(-31));
		assertEquals(10, t.getParent(8));

	}

	@Test
	public void testToArray() {

		int[] test1 = new int[] { -31, -5, 7, 8, 10, 15 };
		int[] test2 = new int[] { 15, 10, 8, 7, -5, -31 };
		assertArrayEquals(test1, t.toArray(true));
		assertArrayEquals(test2, t.toArray(false));

	}

	@Test
	public void testToArrayPostOrder() {
		
		BinarySearchTree test = new BinarySearchTree();

			test.insert(28);
			test.insert(16);
			test.insert(34);
			test.insert(12);
			test.insert(19);
			test.insert(8);
			test.insert(15);
			test.insert(31);
			test.insert(49);
			test.insert(29);
	
		int[] test1 = new int[] {8,15,12,19,16,29,31,49,34,28 };
		assertArrayEquals(test1, test.toArrayPostOrder());
	}

	@Test
	public void testToArrayPreOrder() {
		
		BinarySearchTree test = new BinarySearchTree();

		test.insert(28);
		test.insert(16);
		test.insert(34);
		test.insert(12);
		test.insert(19);
		test.insert(8);
		test.insert(15);
		test.insert(31);
		test.insert(49);
		test.insert(29);

	int[] test1 = new int[] {28, 16, 12,8, 15, 19, 34, 31, 29, 49 };
	assertArrayEquals(test1, test.toArrayPreOrder());
		
	}

	@Test
	public void testMax() {

		BinarySearchTree t = new BinarySearchTree();
		assertEquals(Integer.MIN_VALUE, t.max());

	}

	@Test
	public void testMin() {

		BinarySearchTree t = new BinarySearchTree();
		assertEquals(Integer.MIN_VALUE, t.min());
	}

	@Test
	public void testToString() {
	
		System.out.println(t.toString());
	}

}
